package com.shaoshaohuo.app.view;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.adapter.SpecificationsGridviewAdapter;
import com.shaoshaohuo.app.entity.Specification;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 规格选择 自定义view
 */
public class SelectSpecificationsView extends LinearLayout {

    private Context context;

    private TextView tv_name;
    private GridView gv_value;
    private SpecificationsGridviewAdapter adapter;
    private Specification name;

    private List<Integer> selectPositions = new ArrayList<>();


    public SelectSpecificationsView(Context context) {
        super(context);
        this.context = context;
        init();
    }

    private void init() {
        View.inflate(context, R.layout.view_select_specifications, this);
        tv_name = (TextView) findViewById(R.id.tv_name);
        gv_value = (GridView) findViewById(R.id.gv_value);
    }

    public void setData(Specification name, List<Specification> values, boolean isSingleSelection) {
        this.name = name;
        adapter = new SpecificationsGridviewAdapter<>(context, values, selectPositions, false);
        gv_value.setAdapter(adapter);

        if (isSingleSelection) {
            tv_name.setText(name.specification_name);
            gv_value.setOnItemClickListener(new SingleOnItemClickListener());
        } else {
            tv_name.setText(name.specification_name + "（可多选）");
            gv_value.setOnItemClickListener(new MultiOnItemClickListener());
        }
    }

    /**
     * 单选监听器
     */
    class SingleOnItemClickListener implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (selectPositions.contains(position)) {
                selectPositions.remove(new Integer(position));
            } else {
                selectPositions.clear();
                selectPositions.add(position);
            }
            adapter.notifyDataSetChanged();
        }
    }

    /**
     * 多选监听器
     */
    class MultiOnItemClickListener implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (selectPositions.contains(position)) {
                selectPositions.remove(new Integer(position));
            } else {
                selectPositions.add(position);
            }
            adapter.notifyDataSetChanged();
        }
    }

    public Map<Specification , List<Specification>> getSelectValues() {
        ArrayList<Specification> specifications = new ArrayList<>();
        if (!selectPositions.isEmpty()) {
            for (Integer i : selectPositions) {
                specifications.add(adapter.getItem(i));
            }
        }
        Map<Specification , List<Specification>> result = new HashMap<>();
        if (!specifications.isEmpty()) {
            result.put(name , specifications);
        }
        return result;
    }


}
